<!DOCTYPE html>
<html>
 <head> 
  <title>JS获取当前时间--闫海的个人博客-芒果小叨</title> 
  <!-- meta tags start --> 
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
  <meta name="Keywords" content="个人博客,闫海个人博客,JS,获取当前时间,js当前时间" /> 
  <meta name="Description" content="JS实现动态显示当前时间(12/24小时制)的方法。" /> 
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" /> 
  <!-- meta tags end -->
  <!-- favorite icon starts -->
  <link rel="shortcut icon" href="/Public/Index/mobile/images/common/favicon.ico" type="image/x-icon" />
  <!-- favorite icon ends -->
  <!-- CSS files start -->
  <link href="/Public/Index/mobile/css/framework.css" rel="stylesheet" type="text/css" media="all" />
  <link href="/Public/Index/mobile/css/colorbox.css" rel="stylesheet" type="text/css" media="all" />
  <link href="/Public/Index/mobile/css/responsive.css" rel="stylesheet" type="text/css" media="screen" />
  <link href="/Public/Index/mobile/css/hidpi.css" rel="stylesheet" type="text/css" media="screen" />
  <link href="/Public/Index/mobile/css/skin.css" rel="stylesheet" type="text/css" media="all" />
  <link href="/Public/Index/mobile/css/custom.css" rel="stylesheet" type="text/css" media="all" />
  <link href="/Public/Index/mobile/css/elements.css" rel="stylesheet" type="text/css" media="all" />
  <link href="/Public/Index/mobile/css/style.css" rel="stylesheet" type="text/css" media="all" />
  
  <!-- CSS files end -->
  <!-- JavaScript files start --> 
  <script type="text/javascript" src="/Public/Index/mobile/grayscale.js"></script>
  <script type="text/javascript" src="/Public/Index/mobile/js/jquery.min.js"></script>
  <script type="text/javascript" src="/Public/Index/mobile/js/effects.jquery-ui.min.js"></script>
  <script type="text/javascript" src="/Public/Index/mobile/js/jquery.nivo-slider.min.js"></script>
  <script type="text/javascript" src="/Public/Index/mobile/js/custom.js"></script>
  <script type="text/javascript" src="/Public/Index/mobile/js/jquery.colorbox.min.js"></script>
  
  <!-- JavaScript files end -->
 </head>
 <body>
  <!-- website wrapper starts -->
  <div class="websiteWrapper"> 
   <!-- main menu wrapper starts --> 
   <div class="mainMenuOuterWrapper"> 
    <a href="#" class="mainMenuCloseButton"></a> 
    <ul class="mainMenuWrapper"> 
     <li class="currentPage"><a href='/mobile.php/index.html' title='首页'>首页</a></li>
      <li><a href='/mobile.php/list/2.html' title='关于我'>关于我<span> </span></a></li><li><a href='/mobile.php/list/3.html' title='时光轴'>时光轴<span> </span></a></li><li><a href='/mobile.php/list/1.html' title='品味人生'>品味人生<span> </span></a></li><li><a href='/mobile.php/list/5.html' title='技术分享'>技术分享<span> </span></a></li>     <li><a href='/mobile.php/index/article/message.html'  title='给我留言'>给我留言</a></li>
    </ul> 
   </div> 
   <!-- main menu wrapper ends --> 

<script type="text/javascript">
  var url = '/mobile.php/article';
  $(function(){
    var id = '3';
    $.post(url+'/addClick',{'id':id},function(data){
        $("#pclick").html('点击数：' + data);
    });
  })
</script>
   <!-- header wrapper starts --> 
   <div class="headerOuterWrapper pageHeaderWrapper"> 
    <div class="headerWrapper"> 
     <a class="mainLogo" href="/mobile.php/index.html"><img src="/Public/Index/mobile/images/mainLogo.png" alt="" /></a> 
     <a href="#" class="mainMenuButton"></a>
    </div>
    <!-- page title starts --> 
    <h5 class="pageTitle">当前位置：
	<a href="/mobile.php/index.html">首页</a>&nbsp;&gt;&nbsp;
	<a href="/mobile.php/list/5.html">技术分享</a>
	&nbsp;&gt;&nbsp;<a href="/mobile.php/list/12.html">javascript</a>
	</h5>    <!-- page title ends --> 
   </div>
   <!-- header wrapper ends --> 
   <!-- page wrapper starts --> 
   <div class="pageWrapper singlePostPageWrapper"> 
    <!-- post content wrapper starts --> 
    <div class="singlePostContentWrapper">
    <h3 class="pageTitle" style="background:#F0F0F0;color:#333;text-align:center;">JS获取当前时间</h3> 
     <div class="pageSpacer"></div> 
     <div class="pageSpacer"></div> 
     <div style="word-break: break-all;" class="phoneContent"><pre class="brush:js;toolbar:false">&lt;SCRIPT&nbsp;LANGUAGE=&quot;JavaScript&quot;&gt;
var&nbsp;myDate&nbsp;=&nbsp;new&nbsp;Date();
myDate.getYear();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//获取当前年份(2位)
myDate.getFullYear();&nbsp;&nbsp;&nbsp;//获取完整的年份(4位,1970-????)
myDate.getMonth();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//获取当前月份(0-11,0代表1月)
myDate.getDate();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//获取当前日(1-31)
myDate.getDay();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//获取当前星期X(0-6,0代表星期天)
myDate.getTime();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//获取当前时间(从1970.1.1开始的毫秒数)
myDate.getHours();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//获取当前小时数(0-23)
myDate.getMinutes();&nbsp;&nbsp;&nbsp;&nbsp;//获取当前分钟数(0-59)
myDate.getSeconds();&nbsp;&nbsp;&nbsp;&nbsp;//获取当前秒数(0-59)
myDate.getMilliseconds();&nbsp;&nbsp;&nbsp;//获取当前毫秒数(0-999)
myDate.toLocaleDateString();&nbsp;&nbsp;&nbsp;&nbsp;//获取当前日期
var&nbsp;mytime=myDate.toLocaleTimeString();&nbsp;&nbsp;&nbsp;&nbsp;//获取当前时间
myDate.toLocaleString(&nbsp;);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//获取日期与时间
if&nbsp;(mytime&lt;&quot;23:30:00&quot;){
&nbsp;&nbsp;&nbsp;alert(mytime);
}
&lt;/SCRIPT&gt;</pre><p><br/>2.</p><pre class="brush:js;toolbar:false">&lt;%&nbsp;//日期转换
java.text.SimpleDateFormat&nbsp;formatter&nbsp;=&nbsp;new&nbsp;java.text.SimpleDateFormat(&quot;yyyy-MM-dd&nbsp;HH:mm:ss&quot;);&nbsp;
java.util.Date&nbsp;currentTime&nbsp;=&nbsp;new&nbsp;java.util.Date();//得到当前系统时间&nbsp;
String&nbsp;str_date1&nbsp;=&nbsp;formatter.format(currentTime);&nbsp;//将日期时间格式化&nbsp;
String&nbsp;str_date2&nbsp;=&nbsp;currentTime.toString();&nbsp;//将Date型日期时间转换成字符串形式&nbsp;
%&gt;</pre><p><br/><span style="font-weight:bold">1. JS实现动态显示当前时间(12/24小时制)</span><br/></p><pre class="brush:js;toolbar:false">&lt;script&gt;
window.onload=function(){
&nbsp;&nbsp;&nbsp;&nbsp;function&nbsp;nowTime(ev,type){
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;ev:显示时间的元素
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;type:时间显示模式.若传入12则为12小时制,不传入则为24小时制
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*/
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//年月日时分秒
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;Y,M,D,W,H,I,S;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//月日时分秒为单位时前面补零
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;function&nbsp;fillZero(v){
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(v&lt;10){v=&#39;0&#39;+v;}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;v;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(function(){
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;d=new&nbsp;Date();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;Week=[&#39;星期天&#39;,&#39;星期一&#39;,&#39;星期二&#39;,&#39;星期三&#39;,&#39;星期四&#39;,&#39;星期五&#39;,&#39;星期六&#39;];
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Y=d.getFullYear();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;M=fillZero(d.getMonth()+1);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;D=fillZero(d.getDate());
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;W=Week[d.getDay()];
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;H=fillZero(d.getHours());
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;I=fillZero(d.getMinutes());
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;S=fillZero(d.getSeconds());
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//12小时制显示模式
&nbsp;&nb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;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;H=&#39;下午&amp;nbsp;00&#39;;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ev.innerHTML=Y+&#39;年&#39;+M+&#39;月&#39;+D+&#39;日&nbsp;&#39;+&#39;&nbsp;&#39;+W+&#39;&amp;nbsp;&#39;+H+&#39;:&#39;+I+&#39;:&#39;+S;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//每秒更新时间
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;setTimeout(arguments.callee,1000);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;})();
&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;//24小时制调用
&nbsp;&nbsp;&nbsp;&nbsp;nowTime(document.getElementById(&#39;time24&#39;));
&nbsp;&nbsp;&nbsp;&nbsp;//12小时制调用
&nbsp;&nbsp;&nbsp;&nbsp;nowTime(document.getElementById(&#39;time12&#39;),12);
}
&lt;/script&gt;</pre><p>调用：<br/></p><pre class="brush:html;toolbar:false">&lt;body&gt;
&lt;span&nbsp;id=&quot;time24&quot;&gt;&lt;/span&gt;
&lt;span&nbsp;id=&quot;time12&quot;&gt;&lt;/span&gt;
&lt;/body&gt;</pre><p><br/></p></div>
     <div class="textBreak"></div> 
    </div> 
    <!-- post content wrapper ends --> 
    <!-- post info starts --> 
    <div class="singlePostInfoWrapper">
     <span class="singleIconWrapper singleIconText iconCalendarDark postInfo postDate">2015-01-04</span>
     <a class="singleIconWrapper singleIconText iconEditDark postInfo postAuthor postInfoNoMargin">小叨</a>&nbsp;&nbsp;
     <span class="singleIconWrapper singleIconText iconArrowRightDark" id="pclick">点击数：151</span>
    </div> 
    <!-- post info ends --> 
    <!-- post links wrapper starts --> 
    <div class="singlePostContentWrapper"> 
     <p>上一篇：已经是第一篇文章了</p>
     <p>下一篇：<a href="/mobile.php/article/20.html">JS写一个浮动广告</a></p>
    </div> 
    <!-- post links wrapper ends --> 
    <!-- comments section wrapper starts --> 
    <div class="commentsSectionWrapper"> 
     <h4 class="commentsTitle">相关阅读：</h4> 
     <!-- comments wrapper starts --> 
     <!-- accordion item wrapper starts --> 
        <div class="accordionContent"> 
         <p><a href="/mobile.php/article/36.html" title="jQuery解决跨域问题">jQuery解决跨域问题</a></p><p><a href="/mobile.php/article/31.html" title="关于网站限制ie10以下浏览器访问的探讨">关于网站限制ie10以下浏览器访问的探讨</a></p><p><a href="/mobile.php/article/24.html" title="jquery拖拽效果原理">jquery拖拽效果原理</a></p><p><a href="/mobile.php/article/22.html" title="JS实现窗口垂直水平居中">JS实现窗口垂直水平居中</a></p><p><a href="/mobile.php/article/21.html" title="首页焦点图轮播代码示例">首页焦点图轮播代码示例</a></p><p><a href="/mobile.php/article/20.html" title="JS写一个浮动广告">JS写一个浮动广告</a></p>        </div> 
     <!-- accordion item wrapper ends -->
     <!-- coments wrapper ends --> 
    </div> 
    <!-- comments section wrapper ends --> 
   </div> 
   <!-- page wrapper ends --> 
   <!-- footer wrapper starts --> 
   <div class="footerWrapper"> 
    <!-- copyright wrapper starts --> 
    <div class="copyrightWrapper"> 
     <!-- copyright starts --> 
     <span class="copyright"><a href="http://www.miitbeian.gov.cn/">鲁ICP备15004472号-1</a></span> 
     <!-- copyright ends --> 
     <!-- back to top button starts --> 
     <a href="" class="backToTopButton"></a> 
     <!-- back to top button ends --> 
    </div> 
    <!-- copyright wrapper ends --> 
   </div> 
   <!-- footer wrapper ends --> 
  </div>
  <!-- website wrapper ends -->
 </body>
</html>